package pl.prv.polanie.client.widgets.schedule.custom;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ContextSettings;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;


public class SignInWidgets extends Composite {
	
	private static final String STYLE__SIGN_IN_WIDGETS = "custom-SignInWidgets";
	private static final String STYLE__LOGGED_ADMIN_USER_LINK = "custom-LoggedAdminUserLink";
	
	private HorizontalPanel mainHP = new HorizontalPanel();
	
	private HTML cacheReport = new HTML("<span class='plan-Link'>Cache report</span>");
	private HTML separator1 = new HTML("<span style='color: #aaaaaa'>|</span>");
	private HTML signInLink = new HTML();
	private HTML separator2 = new HTML("<span style='color: #aaaaaa'>|</span>");
	private HTML signedUpAdminUserLoginHTML = new HTML("");
	
	
	public SignInWidgets() {
		initWidget(mainHP);
		mainHP.addStyleName(STYLE__SIGN_IN_WIDGETS);
		
		//CACHE REPORT LINK
		if (ContextSettings.isGuiDebugMode()) {
			mainHP.add(cacheReport);
			cacheReport.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					CacheDebugPanel cdp = new CacheDebugPanel(new Cache());
					cdp.setText("Cache report");
					cdp.center();
				}
			});
			mainHP.add(separator1);
		}
		
		mainHP.add(signInLink);
		signInLink.addStyleName("custom-SignInLink");
		
		
		signInLink.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (ContextSettings.isAdminUserSignedIn()) {
					Planner.get().runOnSignOutAdminUser();
				} else {
					Planner.get().loadLoginForm();
				}
			}
		});
		
		signedUpAdminUserLoginHTML.addStyleName(STYLE__LOGGED_ADMIN_USER_LINK);
		
		if (ContextSettings.isAdminUserSignedIn()) {
			rebuildOnSignIn(ContextSettings.getSignedInAdminUserLogin());
		} else {
			rebuildOnSignOut();
		}
	}
	
	
	public void rebuildOnSignIn(String login) {
		signInLink.setHTML("<span class='plan-Link'>Sign out</span>");
		mainHP.insert(separator2, 0);
		
		signedUpAdminUserLoginHTML.setHTML(login);
		mainHP.insert(signedUpAdminUserLoginHTML, 0);
	}
	
	public void rebuildOnSignOut() {
		
		signInLink.setHTML("<span class='plan-Link'>Sign in</span>");
		
		mainHP.remove(separator2);
		mainHP.remove(signedUpAdminUserLoginHTML);
	} 

}


